package videoDuration.V2;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import it.sauronsoftware.jave.Encoder;

public class URLRead {

	public static void URLRead() throws IOException, Exception{
        String temFile = "D:\\springUpload\\v\\rmvideoV4.mp4";
		URL url = new URL("http://clips.vorwaerts-gmbh.de//big_buck_bunny.mp4");
		URLConnection con = null;
		con = url.openConnection();		
		InputStream is = con.getInputStream();
		FileOutputStream fo = new FileOutputStream(temFile);
		//获取自己数组  
        byte[] data = readInputStream(is); 
        fo.write(data);
        fo.flush();
        fo.close();
        
//        it.sauronsoftware.jave.MultimediaInfo m = new Encoder().getInfo(new File(temFile));
//		long ls = m.getDuration();
//		System.out.println("此视频时长为:"+ls/60000+"分"+(ls)/1000+"秒！");
//		File source = new File("http://clips.vorwaerts-gmbh.de//big_buck_bunny.mp4");
	}
	
	/**
	 * 把字节数组转换成16进制字符串
	 * 
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}
	
	  /** 
	    * @author hsj
	    * @date 2018-05-25
	   	* 从输入流中获取字节数组 
	   	* @param inputStream 
	   	* @return 
	   	* @throws IOException 
	   	*/  
  public static  byte[] readInputStream(InputStream inputStream) throws IOException {    
      byte[] buffer = new byte[1024];    
      int len = 0;    
      ByteArrayOutputStream bos = new ByteArrayOutputStream();    
      while((len = inputStream.read(buffer)) != -1) {    
          bos.write(buffer, 0, len);    
      }    
      bos.close();    
      return bos.toByteArray();    
  } 
	
	public static void main(String[] args) throws Exception{
		URLRead();
	}
	
	
}